home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8191 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple Loop answer
  5. Date: 1 Mar 1996 15:07:27 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7vvfINNnj4@anvil.ugrad.cs.ubc.ca>
  8. References: <4h6dnf$fts@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h6dnf$fts@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
  12.  >Here is what I finally came up with.........
  13.  
  14. You obviously did not read the replies.
  15.  
  16.  
  17.  >#include <stdio.h>
  18.  >#define MAX 1000
  19.  >
  20.  >long int i, j, k, l;
  21.  >long int count = 0;
  22.  >long int compares = 0;
  23.  >
  24.  >int
  25.  >main (void)
  26.  >{
  27.  > 
  28.  >
  29.  >    for     (i = 1; i <= MAX - 3; i++)
  30.  >    for    (j = i + 1; j <= MAX - 2; j++)
  31.  >    for    (k = j + 1; k <= MAX - 1; k++)
  32.  
  33. Having a loop in 'l', and then computing the triplets for increasing 'l' is
  34. probably a more useful.
  35.  
  36.  >    {
  37.  >        l = i + j + k;
  38.  >        compares++;
  39.  >    
  40.  >    if (((0 < i) && (i < j)) && ((j < k)  && (k < l)))
  41.  
  42. This is silly. What does the initialization for your i loop say? Can i _ever_
  43. be equal to, or less than zero?
  44.  
  45. What does your initialization for the j loop say? Can it ever be lower than, or
  46. equal to i?
  47.  
  48. Can k ever be <= j?
  49.  
  50. Why do you bother checking conditions that are already satisfied by your loop
  51. controls?
  52.  
  53.  >       {    count++;
  54.  >    if (compares % 1000000 == 0)
  55.  
  56. The original statement of the problem does not mention how the triplets are
  57. to be ordered. Thus taking every millionth one is meaningless, since such a
  58. sequence depends on the order in which the triplets are computed. You should
  59. document this.
  60. -- 
  61.  
  62.